//
//  FBPayOrderViewController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/3.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "FBPayOrderViewController.h"
#import "FBTradeModel.h"
#import "ChatViewController.h"
#import "FBFindorderModel.h"
#import "UIImageView+WebCache.h"
#import "SDChatDataModel.h"
#import "MyMoneyViewController.h"
/*
 * 状态页面
 */
#import "FBOrderViewStatus_DaiFuKuan.h"
#import "FBOrderViewStatus_OrderCancel.h"
#import "FBOrderViewStatus_OrderAppeal.h"
#import "FBOrderViewStatus_OrderFinish.h"
#import "FBOrderViewStatus_OrderWaitingFangBi_AliPayOrWechat.h"
#import "FBOrderViewStatus_OrderWaitingFangBi_YHK.h"
#import "FBFinderOrderViewStatus_One_Sale_AliPayOrWechat.h"
#import "FBPayYHKView.h"

#import "FBPayAliOrWechatView.h"

#import "WKFangBiPopView.h"
#import "DeclareAbnormalAlertView.h"



@interface FBPayOrderViewController ()<DeclareAbnormalAlertViewDelegate,WKFangBiPopViewDelegate>

@property (nonatomic,strong)UIView * sbgView;

@property (nonatomic,strong)FBFindorderModel * FindOrderModel;

//订单状态，@0：待付款；@1：已付款（未释放代币）；@2：已释放代币；@3：申诉中；@4：已取消
@property (nonatomic,strong)FBOrderViewStatus_DaiFuKuan * FB_View_0;
@property (nonatomic,strong)FBPayAliOrWechatView  * FB_View_0_AlipayOrWechat;
@property (nonatomic,strong)FBPayYHKView *FB_View_0_YHK;

@property (nonatomic,strong)FBOrderViewStatus_OrderWaitingFangBi_AliPayOrWechat * FB_View_1_AlipayOrWechat;
@property (nonatomic,strong)FBOrderViewStatus_OrderWaitingFangBi_YHK * FB_View_1_YHK;

@property (nonatomic,strong)FBFinderOrderViewStatus_One_Sale_AliPayOrWechat * FB_View_1_SaleAlipayOrWechat;

@property (nonatomic,strong)FBOrderViewStatus_OrderFinish * FB_View_2;
@property (nonatomic,strong)FBOrderViewStatus_OrderAppeal * FB_View_3;
@property (nonatomic,strong)FBOrderViewStatus_OrderCancel * FB_View_4;


@property (nonatomic,assign)FBPayOrderVCOrderStatusType Status;

@property (nonatomic, strong) NSTimer * orderTimer;
@property (nonatomic, strong) NSTimer * djsTimer;
@property (nonatomic, strong) NSTimer * unReadOrder_Timer;
@property (nonatomic, assign) NSInteger T;
//选择的支付方式
@property (nonatomic, assign) FBPayType  payType;
@property (nonatomic, strong) NSString * strPaytypeID;

@property (nonatomic, strong) UIView * QR;

@property (nonatomic, strong) UIAlertView *payAlert;
@property (nonatomic, strong) UIAlertView *cancelAlert;
@end

@implementation FBPayOrderViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setProperty];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    [self Http_otcorder_findorder];
    [self setUp_unReadOrder_Timer];
}

-(void)setProperty{
    
    self.view.backgroundColor=RGB(255, 255, 255);
    _strPaytypeID=@"";
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
}

-(void)INIT{
     [_sbgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_sbgView removeFromSuperview];
    _sbgView = [[UIView alloc]init];
    [self.bgView addSubview:_sbgView];
    [_sbgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.bgView);
    }];
}

#pragma mark-创建UI
-(void)cerateUI{
    [self INIT];
    NSString * status = [NSString stringWithFormat:@"%@",self.FindOrderModel.status];
//订单状态 @0：待付款；@1：已付款（未释放代币）；@2：已释放代币；@3：申诉中；@4：已取消
    if ([status integerValue]==0) {
        [self create_UI_status_0];
    }else if ([status integerValue]==1){
        [self create_UI_status_1];
    }else if ([status integerValue]==2){
        [self create_UI_status_2];
    }else if ([status integerValue]==3){
        [self create_UI_status_3];
        [self Http_otcorder_serviceQq];
    }else if ([status integerValue]==4){
        [self create_UI_status_4];
    }
}

//订单状态    订单状态，@0：待付款；@1：已付款（未释放代币）；@2：已释放代币；@3：申诉中；@4：已取消
#pragma mark-待付款-------0
-(void)create_UI_status_0{
    WS(weakSelf);
    self.baseVcTitle = @"待付款";
    [_sbgView addSubview:self.FB_View_0];
    [self.FB_View_0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.sbgView);
    }];
    self.FB_View_0.block = ^(FBFindorderActionType Type) {
        [weakSelf DoSomethingWithType:Type];
    };
    self.FB_View_0.paySelcetedBlock = ^(FBPayType payType) {
        weakSelf.payType = payType;
    };
    self.FB_View_0.dataSource=self.FindOrderModel;
    [self getTime];//开启倒计时
}

#pragma mark-支付-微信/支付宝
-(void)create_UI_status_0_AlipayOrWechat{
    self.baseVcTitle = @"付款中";
    WS(weakSelf);
    [_sbgView addSubview:self.FB_View_0_AlipayOrWechat];
    [self.FB_View_0_AlipayOrWechat mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.sbgView);
    }];
    self.FB_View_0_AlipayOrWechat.payType = self.payType;
    self.FB_View_0_AlipayOrWechat.dataSource=self.FindOrderModel;
    self.FB_View_0_AlipayOrWechat.block = ^(FBFindorderActionType Type) {
        [weakSelf DoSomethingWithType:Type];
    };
    [self getTime];
}


#pragma mark-支付-银行卡
-(void)create_UI_status_0_YHK{
    self.baseVcTitle = @"付款中";
    WS(weakSelf);
    [_sbgView addSubview:self.FB_View_0_YHK];
    [self.FB_View_0_YHK mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.sbgView);
    }];
    self.FB_View_0_YHK.payType = self.payType;
    self.FB_View_0_YHK.dataSource=self.FindOrderModel;
    self.FB_View_0_YHK.block = ^(FBFindorderActionType Type) {
        [weakSelf DoSomethingWithType:Type];
    };
    [self getTime];
}

#pragma mark-已付款（未释放代币）
-(void)create_UI_status_1{
    self.baseVcTitle = @"";
    WS(weakSelf);
    //购买
    if ([FBFindorderTool GetBugStatusWithModel:self.FindOrderModel]) {
        //银行卡
        if (_payType == FBPayType_YHK) {
            [_sbgView addSubview:self.FB_View_1_YHK];
            [self.FB_View_1_YHK mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.equalTo(self.sbgView);
            }];
            self.FB_View_1_YHK.payType = self.payType;
            self.FB_View_1_YHK.dataSource=self.FindOrderModel;
            [self getTime];//开启倒计时
            self.FB_View_1_YHK.block = ^(FBFindorderActionType Type) {
                [weakSelf DoSomethingWithType:Type];
            };
        //支付宝微信
        }else{
            [_sbgView addSubview:self.FB_View_1_AlipayOrWechat];
            [self.FB_View_1_AlipayOrWechat mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.equalTo(self.sbgView);
            }];
            self.FB_View_1_AlipayOrWechat.payType = self.payType;           self.FB_View_1_AlipayOrWechat.dataSource=self.FindOrderModel;
            [self getTime];//开启倒计时
            self.FB_View_1_AlipayOrWechat.block = ^(FBFindorderActionType Type) {
                [weakSelf DoSomethingWithType:Type];
            };
        }
    }else{
        [_sbgView addSubview:self.FB_View_1_SaleAlipayOrWechat];
        [self.FB_View_1_SaleAlipayOrWechat mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(self.sbgView);
        }];
     self.FB_View_1_SaleAlipayOrWechat.dataSource=self.FindOrderModel;
        self.FB_View_1_SaleAlipayOrWechat.block = ^(FBFindorderActionType Type) {
            [weakSelf DoSomethingWithType:Type];
        };
         
        [self getTime];//开启倒计时
    }
}

#pragma mark-订单已经完成
-(void)create_UI_status_2{
    WS(weakSelf);
    self.baseVcTitle = @"已完成";
    [_sbgView addSubview:self.FB_View_2];
    [self.FB_View_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.sbgView);
    }];
    self.FB_View_2.dataSource=self.FindOrderModel;
    self.FB_View_2.block = ^(FBFindorderActionType Type) {
        [weakSelf DoSomethingWithType:Type];
    };
    
}

#pragma mark-订单申诉
-(void)create_UI_status_3{
    WS(weakSelf);
    self.baseVcTitle = @"订单申诉";
    [_sbgView addSubview:self.FB_View_3];
    [self.FB_View_3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.sbgView);
    }];
    self.FB_View_3.block = ^(FBFindorderActionType Type) {
        [weakSelf DoSomethingWithType:Type];
    };
    self.FB_View_3.dataSource=self.FindOrderModel;
}

#pragma mark-订单取消的状态
-(void)create_UI_status_4{
    WS(weakSelf);
    self.baseVcTitle = @"订单已取消";
    [_sbgView addSubview:self.FB_View_4];
    [self.FB_View_4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.sbgView);
    }];
    self.FB_View_4.block = ^(FBFindorderActionType Type) {
        [weakSelf DoSomethingWithType:Type];
    };
    self.FB_View_4.dataSource=self.FindOrderModel;
}

#pragma mark-根据类型做事情
-(void)DoSomethingWithType:(FBFindorderActionType)Type{
    if (Type == FBFindorderActionType_GoToChat){
        [self GoToChatVC];
    }else if (Type == FBFindorderActionType_GoToMoney){
        [self GoToMyMoneyVC];
    }else if (Type == FBFindorderActionType_ShenSu) {
        [self DidShenSu];
    }else if (Type == FBFindorderActionType_FangBi){
        WKFangBiPopView *alertView = [[WKFangBiPopView alloc]initWithTitle:@"提示" message:@"" delegate:self leftButtonTitle:@"我再想想" rightButtonTitle:@"确定放行"];
        [alertView show];
    }else if (Type == FBFindorderActionType_LookQRcode) {
        [self openQRcode];
    }else if (Type == FBFindorderActionType_DidPay){
        //确定付款
        [self FBFindorderAction_DidPay];
    }else if (Type == FBFindorderActionType_CancelOrder){
        [self FBFindorderAction_CancelOrder];
    //支付
    }else if (Type == FBFindorderActionType_GoToPay){
        if (self.payType<1) {
            [HUD showErrorMessage:@"请选择支付方式！"];
            return ;
        }
        [self INIT];
        if (self.payType == FBPayType_YHK) {
            NSDictionary * BankCard = self.FindOrderModel.BankCard;
            self.strPaytypeID = BankCard[@"paytype_id"];
        }else if (self.payType == FBPayType_Wechat){
            NSDictionary * WechatPay = self.FindOrderModel.WechatPay;
            self.strPaytypeID = WechatPay[@"paytype_id"];
        }else if (self.payType == FBPayType_ALiPay){
            NSDictionary * ALiPay = self.FindOrderModel.ALiPay;
            self.strPaytypeID = ALiPay[@"paytype_id"];
        }
        if (self.payType == FBPayType_YHK) {
            [self create_UI_status_0_YHK];
        }else{
            [self create_UI_status_0_AlipayOrWechat];
        }
    }
}

#pragma mark-跳转聊天页面
-(void)GoToChatVC{
    
    ChatViewController * VC = [[ChatViewController alloc]init];
    VC.order_sn = [NSString stringWithFormat:@"%@",self.FindOrderModel.order_sn];
    VC.order_id = [NSString stringWithFormat:@"%@",self.FindOrderModel.ID];
    VC.FindOrderModel= self.FindOrderModel;
    SDChatDataModel * SD = [SDChatDataModel sharedSDChatDataModel];
    SD.FindorderModel = self.FindOrderModel;
    [self.navigationController pushViewController:VC animated:YES];
    
}
#pragma mark-跳转资产页面
-(void)GoToMyMoneyVC{
    MyMoneyViewController * VC = [[MyMoneyViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark-创建定时器
- (void)setUpTimer{
    [self stopTimer];
    WS(wself);
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:wself selector:@selector(Http_otcorder_orderStatus) userInfo:nil repeats:YES];
    self.orderTimer = timer;
    [[NSRunLoop mainRunLoop ] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)setUp_djsTimer{
    [self stop_djsTimer];
    WS(wself);
    NSTimer * djsTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:wself selector:@selector(djsAction) userInfo:nil repeats:YES];
    self.djsTimer = djsTimer;
    [[NSRunLoop mainRunLoop ] addTimer:djsTimer forMode:NSRunLoopCommonModes];
}

-(void)setUp_unReadOrder_Timer{
    [self stop_unReadOrder_Timer];
    WS(wself);
    NSTimer * unReadOrder_Timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:wself selector:@selector(Http_otcorder_unreadorder) userInfo:nil repeats:YES];
    self.unReadOrder_Timer = unReadOrder_Timer;
    [[NSRunLoop mainRunLoop ] addTimer:unReadOrder_Timer forMode:NSRunLoopCommonModes];
}

-(void)getTime{
    
    NSInteger T1 = [self.FindOrderModel.over_time integerValue];
    NSInteger T2 = [[MOLoadHttpsData getCurrentTimestamp] integerValue];

    _T = T1 - T2;
    if (_T<0) {
        _T=0;
        [self stop_djsTimer];
        self.FB_View_0.time.text=@"0分0秒";
        self.FB_View_0_AlipayOrWechat.sTime=@"0分0秒";
        self.FB_View_0_YHK.sTime=@"0分0秒";
        self.FB_View_1_YHK.time.text=@"0分0秒";
        self.FB_View_1_AlipayOrWechat.time.text = @"0分0秒";
        self.FB_View_1_SaleAlipayOrWechat.time.text = @"0分0秒";
    }else{
        [self setUp_djsTimer];
    }
}

-(void)djsAction{
    
    _T--;
    if (_T<0||_T==0) {
        [self stop_djsTimer];
    }else{
        self.FB_View_0.time.text = [MOLoadHttpsData getMMSSFromSS:_T];
        self.FB_View_1_AlipayOrWechat.time.text = [MOLoadHttpsData getMMSSFromSS:_T];
        self.FB_View_1_SaleAlipayOrWechat.time.text = [MOLoadHttpsData getMMSSFromSS:_T];
        self.FB_View_0_AlipayOrWechat.sTime = [MOLoadHttpsData getMMSSFromSS:_T];
        self.FB_View_0_YHK.sTime = [MOLoadHttpsData getMMSSFromSS:_T];
        self.FB_View_1_YHK.time.text = [MOLoadHttpsData getMMSSFromSS:_T];

    }
}
- (void)stopTimer{
    //关闭定时器
    [self.orderTimer invalidate];
    self.orderTimer = nil;
}
- (void)stop_djsTimer{
    [self.djsTimer invalidate];
    self.djsTimer = nil;
}

-(void)stop_unReadOrder_Timer{
    [self.unReadOrder_Timer invalidate];
    self.unReadOrder_Timer = nil;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self stopTimer];
    [self stop_djsTimer];
    [self stop_unReadOrder_Timer];
    [self INIT];
}
- (void)dealloc{
    [self stopTimer];
    [self stop_djsTimer];
    [self stop_unReadOrder_Timer];
}


#pragma mark--获取指定的订单信息（下单后调用）
-(void)Http_otcorder_findorder{

    
    NSString * URL = [NSString stringWithFormat:@"%@otcorder/findorder",DEFAULTSEVERBASEURL];
    NSString * order_sn = _order_sn;

    NSDictionary * Dic  = @{
                            @"order_sn":order_sn,
                            @"access_token":[LoginModel GetValue:DL_token]
                            };
    WS(weakSelf);
    [HUD showProgress:@"" inView:self.bgView];
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        [HUD dismissInView:weakSelf.bgView];
        
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        if ([code isEqualToString:@"0"]){
            
            NSDictionary * dicc = dic[@"data"];
//            NSLog(@"-------%@",dicc);
            weakSelf.FindOrderModel = [FBFindorderModel mj_objectWithKeyValues:dicc];
            weakSelf.Status = [FBFindorderTool GetTypeWithSatus:[weakSelf.FindOrderModel.status integerValue]];
            if (weakSelf.FindOrderModel.payment_info) {
                NSString * paytype_id = [NSString stringWithFormat:@"%@",weakSelf.FindOrderModel.payment_info[@"paytype_id"]] ;
                NSString * symbol = [NSString stringWithFormat:@"%@",weakSelf.FindOrderModel.payment_info[@"symbol"]] ;

                if ((![paytype_id containsString:@"null"]) && paytype_id.length>0) {
                    weakSelf.strPaytypeID = paytype_id;
                    if ([symbol containsString:@"支付宝"]) {
                        weakSelf.payType = FBPayType_ALiPay;
                    }else if ([symbol containsString:@"微信"]){
                        weakSelf.payType = FBPayType_Wechat;
                    }else if ([symbol containsString:@"银行卡"]){
                        weakSelf.payType = FBPayType_YHK;
                    }
                    
                }
            }
            [weakSelf cerateUI];
            /*
             * 开始轮询  订单状态
             */
            [weakSelf setUpTimer];
        }else{
            [HUD showErrorMessage:msg];
        }
        
    } FailureBlock:^(id  _Nonnull error) {
        [HUD dismissInView:weakSelf.bgView];

    }];

}
#pragma mark-获取客服qq
-(void)Http_otcorder_serviceQq{
    
    NSString * URL = [NSString stringWithFormat:@"%@otcorder/serviceQq",DEFAULTSEVERBASEURL];

    NSDictionary * Dic  = @{
                            @"access_token":[LoginModel GetValue:DL_token]
                            };
    WS(weakSelf);
    [HUD showProgress:@"" inView:self.bgView];
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        [HUD dismissInView:weakSelf.bgView];
        
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        if ([code isEqualToString:@"0"]){
            
            NSDictionary * dicc = dic[@"data"];
            
            weakSelf.FB_View_3.sevQQ = [NSString stringWithFormat:@"%@",dicc[@"service_qq"]];
//            NSLog(@"-------%@",dicc);
            
        }else{
            [HUD showErrorMessage:msg];
        }
        
    } FailureBlock:^(id  _Nonnull error) {
        [HUD dismissInView:weakSelf.bgView];
        
    }];

}

#pragma mark-标记订单为我已付款
-(void)Http_otcorder_orderpay{
    
    NSString * URL = [NSString stringWithFormat:@"%@otcorder/orderpay",DEFAULTSEVERBASEURL];
    
    NSDictionary * Dic  = @{
                            @"order_sn":self.FindOrderModel.order_sn,
                            @"pay_type":self.strPaytypeID,
                            @"access_token":[LoginModel GetValue:DL_token]
                            };
    WS(weakSelf);
    [HUD showProgress:@"" inView:self.bgView];
    
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        [HUD dismissInView:weakSelf.bgView];
        
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        //标记付款成功后  刷新UI
        if ([code isEqualToString:@"0"]){
            [weakSelf Http_otcorder_findorder];
        }else{
            NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [HUD showErrorMessage:msg];
        }
        
    } FailureBlock:^(id  _Nonnull error) {
        [HUD dismissInView:weakSelf.bgView];
    }];
}

#pragma mark-获取订单状态-otcorder/orderStatus
-(void)Http_otcorder_orderStatus{
    
    NSString * URL = [NSString stringWithFormat:@"%@otcorder/orderStatus",DEFAULTSEVERBASEURL];
    
    NSDictionary * Dic  = @{
                            @"order_sn":self.FindOrderModel.order_sn,
                            @"access_token":[LoginModel GetValue:DL_token]
                            };
    WS(weakSelf);
//    [HUD showProgress:@"" inView:self.bgView];
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
//        [HUD dismissInView:weakSelf.bgView];
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]){
            NSDictionary * dicc = dic[@"data"];
            NSString * status = [NSString stringWithFormat:@"%@",dicc[@"status"]];
            [weakSelf CompareStatusWithStatus:[status integerValue]];
        }
    } FailureBlock:^(id  _Nonnull error) {
//        [HUD dismissInView:weakSelf.bgView];
        
    }];
    
}

#pragma mark-发放代币
-(void)Http_otcorder_orderSend:(NSString *)asset_password{
    
    NSString * URL = [NSString stringWithFormat:@"%@otcorder/orderSend",DEFAULTSEVERBASEURL];
    
    NSDictionary * Dic  = @{
                            @"order_sn":self.FindOrderModel.order_sn,
                                @"asset_password":asset_password,
                            @"access_token":[LoginModel GetValue:DL_token]
                            };
    WS(weakSelf);
    [HUD showProgress:@"" inView:self.bgView];
    
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
                [HUD dismissInView:weakSelf.bgView];
        
        NSDictionary * dic = responseObject;
        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]){
            [weakSelf Http_otcorder_findorder];
        }else{
            [HUD showErrorMessage:msg];
        }
        
        
    } FailureBlock:^(id  _Nonnull error) {
                [HUD dismissInView:weakSelf.bgView];
    }];
}

#pragma mark-取消订单
-(void)Http_otcorder_cancelOrder{
    
    NSString * URL = [NSString stringWithFormat:@"%@otcorder/cancelOrder",DEFAULTSEVERBASEURL];
    
    NSDictionary * Dic  = @{
                            @"order_sn":self.FindOrderModel.order_sn,
                            @"access_token":[LoginModel GetValue:DL_token]
                            };
    WS(weakSelf);
    [HUD showProgress:@"" inView:self.bgView];
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        [HUD dismissInView:weakSelf.bgView];
        NSDictionary * dic = responseObject;
        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]){
            [weakSelf Http_otcorder_findorder];
        }else{
            [HUD showErrorMessage:msg];
        }
    } FailureBlock:^(id  _Nonnull error) {
        [HUD dismissInView:weakSelf.bgView];
    }];
}



#pragma mark-获取最新订单数量
-(void)Http_otcorder_unreadorder{
    
    NSString * URL = [NSString stringWithFormat:@"%@ex/otcorder/unreadorder",DEFAULTSEVERBASEURL];
    
    NSDictionary * Dic  = @{
                            @"order_sn":self.FindOrderModel.order_sn
                            };
    WS(weakSelf);
    [MOLoadHttpsData GetHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        NSDictionary * dic = responseObject;
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]){
            if ([dic[@"data"] isKindOfClass:[NSDictionary class]]) {
                FBFindorderUnReadOrder * FU = [FBFindorderUnReadOrder sharedFBFindorderUnReadOrder];
                FU.UnReadOrderDic = dic[@"data"];
                [weakSelf.FB_View_0 setUnReadOrderNum];
                [weakSelf.FB_View_0_AlipayOrWechat setUnReadOrderNum];
                [weakSelf.FB_View_0_YHK setUnReadOrderNum];
                [weakSelf.FB_View_1_AlipayOrWechat setUnReadOrderNum];
                [weakSelf.FB_View_1_YHK setUnReadOrderNum];
                [weakSelf.FB_View_0 setUnReadOrderNum];
                
                [weakSelf.FB_View_1_SaleAlipayOrWechat setUnReadOrderNum];
                [weakSelf.FB_View_2 setUnReadOrderNum];
                [weakSelf.FB_View_3 setUnReadOrderNum];
                [weakSelf.FB_View_4 setUnReadOrderNum];
            }
        }
    } FailureBlock:^(id  _Nonnull error) {
        
    }];

}

#pragma mark-申诉
-(void)DidShenSu{
    DeclareAbnormalAlertView *alertView = [[DeclareAbnormalAlertView alloc]initWithTitle:@"订单申诉" message:@"申诉理由200字以内，申诉后该订单将会被冻结" delegate:self leftButtonTitle:@"确定" rightButtonTitle:@"取消"];
    [alertView show];
}

#pragma mark-订单申诉
-(void)Http_otcorder_orderappeal:(NSString *)reason{
    
    NSString * URL = [NSString stringWithFormat:@"%@otcorder/orderappeal",DEFAULTSEVERBASEURL];
    
    NSDictionary * Dic  = @{
                            @"order_sn":self.FindOrderModel.order_sn,
                            @"reason":reason,
                            @"access_token":[LoginModel GetValue:DL_token]
                            };
    WS(weakSelf);
    [HUD showProgress:@"" inView:self.bgView];
    
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        [HUD dismissInView:weakSelf.bgView];
        
        NSDictionary * dic = responseObject;
        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]){
            [weakSelf Http_otcorder_findorder];
        }else{
            [HUD showErrorMessage:msg];
        }
    } FailureBlock:^(id  _Nonnull error) {
        [HUD dismissInView:weakSelf.bgView];
        
    }];
    
}

#pragma mark-比较状态
-(void)CompareStatusWithStatus:(NSInteger)status{
    FBPayOrderVCOrderStatusType Type = [FBFindorderTool GetTypeWithSatus:status];
    if (_Status == Type) {
    //状态变了就重新刷新
    }else{
        [self Http_otcorder_findorder];
    }
}

#pragma mark===================懒加载===================
- (FBOrderViewStatus_DaiFuKuan *)FB_View_0{
    if (!_FB_View_0) {
        _FB_View_0 = [[FBOrderViewStatus_DaiFuKuan alloc]init];
    }
    return  _FB_View_0;
}

- (FBPayYHKView *)FB_View_0_YHK{
    if (!_FB_View_0_YHK) {
        _FB_View_0_YHK = [[FBPayYHKView alloc]init];
    }
    return  _FB_View_0_YHK;
}

-(FBOrderViewStatus_OrderWaitingFangBi_AliPayOrWechat *)FB_View_1_AlipayOrWechat{
    if (!_FB_View_1_AlipayOrWechat) {
        _FB_View_1_AlipayOrWechat = [[FBOrderViewStatus_OrderWaitingFangBi_AliPayOrWechat alloc]init];
    }
    return  _FB_View_1_AlipayOrWechat;
}
-(FBOrderViewStatus_OrderWaitingFangBi_YHK *)FB_View_1_YHK{
    if (!_FB_View_1_YHK) {
        _FB_View_1_YHK = [[FBOrderViewStatus_OrderWaitingFangBi_YHK alloc]init];
    }
    return  _FB_View_1_YHK;
}

-(FBFinderOrderViewStatus_One_Sale_AliPayOrWechat *)FB_View_1_SaleAlipayOrWechat{
    if (!_FB_View_1_SaleAlipayOrWechat) {
        _FB_View_1_SaleAlipayOrWechat=[[FBFinderOrderViewStatus_One_Sale_AliPayOrWechat alloc]init];
    }
    return _FB_View_1_SaleAlipayOrWechat;
}
-(FBOrderViewStatus_OrderFinish *)FB_View_2{
    if (!_FB_View_2) {
        _FB_View_2 = [[FBOrderViewStatus_OrderFinish alloc]init];
    }
    return _FB_View_2;
}
-(FBOrderViewStatus_OrderAppeal *)FB_View_3{
    if (!_FB_View_3) {
        _FB_View_3 = [[FBOrderViewStatus_OrderAppeal alloc]init];
    }
    return _FB_View_3;
}
-(FBOrderViewStatus_OrderCancel *)FB_View_4{
    if (!_FB_View_4) {
        _FB_View_4 = [[FBOrderViewStatus_OrderCancel alloc]init];
    }
    return _FB_View_4;
}

-(FBPayAliOrWechatView *)FB_View_0_AlipayOrWechat{
    if (!_FB_View_0_AlipayOrWechat) {
        _FB_View_0_AlipayOrWechat = [[FBPayAliOrWechatView alloc]init];
    }
    return _FB_View_0_AlipayOrWechat;
}

#pragma mark-==============Alert==============
-(void)FBFindorderAction_DidPay{
    _payAlert = [[UIAlertView alloc] initWithTitle:@"付款确认" message:@"确定您已向卖家付款。恶意点击将直接冻结账户！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    _payAlert.delegate=self;
    _payAlert.alertViewStyle = UIAlertViewStyleDefault;
    [_payAlert show];
}
-(void)FBFindorderAction_CancelOrder{
    _cancelAlert = [[UIAlertView alloc] initWithTitle:@"取消订单" message:@"如果您已经向卖家付款请千万不要取消交易。取消交易规则:买方当日累计3笔取消，将会限制当天的买入功能" delegate:self cancelButtonTitle:@"我再想想" otherButtonTitles:@"确定取消", nil];
    _cancelAlert.delegate=self;
    _cancelAlert.alertViewStyle = UIAlertViewStyleDefault;
    [_cancelAlert show];
}



-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(alertView==_payAlert){
        if (buttonIndex == 1) { //确认
            [self Http_otcorder_orderpay];
        }
    }else if (alertView==_cancelAlert){
        if (buttonIndex == 1) { //确认
            //取消订单
            [self Http_otcorder_cancelOrder];
        }
    }
}

#pragma mark-查看二维码
-(void)openQRcode{
    
    UIView * QR = [[UIView alloc]init];
    [self.view addSubview:QR];
    QR.backgroundColor=[UIColor clearColor];
    [QR mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.bottom.equalTo(self.view);
    }];
    _QR=QR;
    
    UIButton *QRbtn = [[UIButton alloc]init];
    [QR addSubview:QRbtn];
    [QRbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.bottom.equalTo(QR);
    }];
    QRbtn.backgroundColor=[UIColor blackColor];
    QRbtn.alpha=0.5;
    
    UIImageView * imgV = [[UIImageView alloc]init];
    [QR addSubview:imgV];
    [imgV setContentMode:UIViewContentModeScaleAspectFill];
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(QR.mas_centerY);
        make.centerX.equalTo(QR.mas_centerX);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(200);
    }];
    NSDictionary * account;
    if (_payType == FBPayType_ALiPay) {
        NSDictionary * ALiPay = self.FindOrderModel.ALiPay;
        account = ALiPay[@"account"];
    }else if (_payType == FBPayType_Wechat){
        NSDictionary * WechatPay = self.FindOrderModel.WechatPay;
        account = WechatPay[@"account"];
    }
    
    if ([account isKindOfClass:[NSDictionary class]]) {
        NSString * URL = [NSString stringWithFormat:@"%@",account[@"qrcode_url"]];
        [imgV sd_setImageWithURL:[NSURL URLWithString:URL]];
    }
    
    [QRbtn addTarget:self action:@selector(disMissQRcode) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)disMissQRcode{
    WS(weakSelf);
    [UIView animateWithDuration:0.3 animations:^{
        [weakSelf.QR removeFromSuperview];
    }];
    
}
#pragma mark-------delegate--------
- (void)declareWKFangBiPopView:(WKFangBiPopView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex andPassword:(NSString *)password{
    if (buttonIndex == WKFangBiAlertButtonLeft) {
        
    }else{
        [self Http_otcorder_orderSend:password];
    }
}
- (void)declareAbnormalAlertView:(DeclareAbnormalAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex andReason:(NSString *)reason{
    if (buttonIndex == AlertButtonLeft) {
    }else{
        [self Http_otcorder_orderappeal:reason];
    }
}
@end
